package com.example.controller;

import com.example.entity.User;
import com.example.service.LikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/like")
public class LikeController {
    @Autowired
    private LikeService likeService;

    // 点赞
    @PostMapping("/{productId}")
    @ResponseBody
    public String like(@PathVariable Long productId, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "请先登录";
        }
        likeService.likeProduct(user.getId(), productId);
        return "success";
    }

    // 取消点赞
    @DeleteMapping("/{productId}")
    @ResponseBody
    public String unlike(@PathVariable Long productId, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "请先登录";
        }
        likeService.unlikeProduct(user.getId(), productId);
        return "success";
    }
} 